package com.test28_类库应用;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 实现数据的统计操作
 */
interface IStat {
    /** 统计报告 */
    public Map<String, Integer> report();
}

/**
 * 扔硬币统计
 */
class CointStat implements IStat {
    private int count;  // 设置总共投掷次数
    private Random random = new Random();
    private Map<String, Integer> map = new HashMap<>();

    public CointStat() {
        this(100);
    }

    public CointStat(int count) {
        this.count = count;
        this.handle();
    }

    /**
     * 投掷处理操作
     */
    private void handle() {
        int up = 0; // 正面次数
        int down = 0;   // 反面次数
        for (int x = 0; x < this.count; x++) {
            if (this.random.nextInt(2) == 1) {
                up++;
            } else {
                down++;
            }
        }
        this.map.put("up", up);
        this.map.put("down", down);
    }

    @Override
    public Map<String, Integer> report() {
        return this.map;
    }
}

class StatFactory {
    private StatFactory() {
    }

    public static IStat getInstance(int count) {
        return new CointStat(count);
    }
}

/**
 * 硬币投掷
 */
public class YootkDemo_08 {

    @Test
    public void test1() {
        IStat stat = StatFactory.getInstance(1000);
        System.out.println("【正面次数】" + stat.report().get("up"));
        System.out.println("【反面次数】" + stat.report().get("down"));
    }
}
